<template>
  <div class="person">
    <h2>情况四：监视ref或reactive定义的【对象类型】数据中的某个属性</h2>
    <h2>姓名：{{ person.name }}</h2>
    <h2>年龄：{{ person.age }}</h2>
    <h2>汽车：{{ person.car.c1 }}----{{ person.car.c2 }}</h2>
    <button @click="handleName">修改姓名</button>
    <button @click="handleAge">修改年龄</button>
    <button @click="handleC1">修改车1</button>
    <button @click="handleC2">修改车2</button>
    <button @click="handlePer">修改整个人</button>
    <button @click="handleCar">修改整个车</button>
  </div>
</template>
  <script setup lang='ts'>
import { reactive, watch } from "vue";
let person = reactive({
  name: "张三",
  age: 18,
  car: {
    c1: "奔驰",
    c2: "宝马"
  }
});
function handleName() {
  person.name += "~";
}
function handleAge() {
  person.age += 1;
}
function handleC1() {
  person.car.c1 = "奥迪";
}
function handleC2() {
  person.car.c2 = "大众";
}
function handlePer() {
  // person = Object.assign(person,{name:"历史",age:123});
  //或者
  Object.assign(person, { name: "历史", age: 123 });
}
function handleCar() {
  person.car = { c1: "雅达", c2: "新日" };
}
//watch("监听谁",回调函数,"深度监听")
1; //监视响应式对象中的某个属性，且该属性为基本类型，要写成函数形式
// watch(()=>{return person.name}, (newV, oldV) => {
// watch(
//   () => person.name,
//   (newV, oldV) => {
//     console.log("person.name变化了", newV, oldV);
//   }
// );

//监视响应式对象中的某个属性，且该属性为对象类型，可以直接写（如2），也能写成函数（如3），更推荐函数形式

2; //监听的细枝末节，car里面的属性，属性变化，会监听到（handleC1，handleC2）事件，单整个对象（car）变化，监听不到，handleCar事件监听不到
// watch(person.car, (newV, oldV) => {
//   console.log("person.car变化了", newV, oldV);
// });
3; //监听整个对象car，情况正好和上面2相反。
// watch(()=>person.car, (newV, oldV) => {
//   console.log("person.car变化了", newV, oldV);
// });
4; //监听--细枝末节和整体都会改变 deep:true
watch(
  () => person.car,
  (newV, oldV) => {
    console.log("person.car变化了", newV, oldV);
  },
  { deep: true }
);
</script>
  <style scoped>
.person {
  background-color: skyblue;
  box-shadow: 0 0 10px;
  border-radius: 10px;
  padding: 20px;
}
</style>